

do "${folder}/1_sumstats_balance_fs/usa_00155.do"

keep if statefip==36 & (countyfip==47 | countyfip==5 | countyfip==61 | countyfip==81 |  countyfip==85) | ///
statefip==17 & (countyfip==31) 

gen st_puma="36"+string(puma, "%05.0f") if statefip==36
replace st_puma="17"+string(puma, "%05.0f") if statefip==17

*** Merge on puma share of eviction filings
merge m:1 st_puma using $build/puma_weights
drop _merge

gen black=(race==2)
gen whitenothisp=(race==1 & hispan==0)

gen qwage=(incwage/4)
gen age1855=(age>=18 & age<56)
gen condqwage=qwage if qwage!=0
gen employed=(qwage>0) 


gen female=(sex==2)
gen poor=(poverty<=100)

tab gqtype

*** Drop Group Quarters
keep if gqtype==0

*** Calculate PUMA "Neighborhood characteristics"
preserve
replace rent=. if ownershp!=2
collapse (mean) pct_poor=poor (p50) med_rent = rent [pw=perwt], by(st_puma)
tempfile nhood
save `nhood'
restore

merge m:1 st_puma using `nhood'

*** Adjust Dollars by CPI
/* 
*** Chicago
*** CPI 2010 = 212.870
*** CPI 2016 = 229.302

*** New York
*** CPI 2010 = 240.864
*** CPI 2016 = 263.365
*/

gen cpi = . 
replace cpi = 263.365/240.864 if nyc==1
replace cpi = 229.302/212.870 if nyc!=1

gen _hisp=(hispan!=0)

replace qwage=qwage*cpi
replace med_rent=med_rent*cpi
replace condqwage=condqwage*cpi

*** Restrict to Renters 18-55
keep if gqtype==0 & ownershp==2
keep if age1855==1
 
*** Construct weights
** First person weights without Census weights
gen persons=1
bys nyc: egen placetotal=total(persons)
bys nyc st_puma: egen pumatotal=total(persons)
gen puma_share_persons=pumatotal/placetotal

gen puma_weight= puma_share_cases/ puma_share_persons

tab st_puma, gen(pumadum)
** Check that those work
mean puma_share_cases if pumadum3==1 & nyc!=1
mean pumadum3 [pw=puma_weight] if nyc!=1

*** Construct weights using census weights
bys nyc: egen totalperwt=total(perwt)
bys nyc st_puma: egen pumatotalperwt=total(perwt)
gen puma_share_perwt=pumatotalperwt/totalperwt

gen puma_perwt_weight= puma_share_cases/ puma_share_perwt
gen p_pumawt=puma_perwt_weight*perwt

** Check that these work
mean puma_share_cases if pumadum3==1 & nyc!=1
mean pumadum3 [pw=p_pumawt] if nyc!=1
 

label var age "Age at Filing"
label var female "Female"
label var black "Black"
label var white "White, Not-Hispanic"

label var pct_poor "Tract Poverty Rate"
label var med_rent "Tract Median Rent"

label var qwage "Earnings Q1-8"
label var employed "Employment Q1-8"
label var condqwage "Earnings Q1-8, Conditional on Employed"

gen place = "nyc" if nyc==1
replace place = "cc" if nyc!=1

drop hispan hispand
rename _hisp hisp 

save "$build/acs_pums_ny_chi", replace

foreach p in nyc cc {

use "$build/acs_pums_ny_chi", clear

keep if place == "`p'"
gen person =1 
total person if age1855==1 & ownershp==2
global bigN "e(N)"
di $bigN

keep if age1855==1 & ownershp==2


preserve

collapse (mean) age female hisp whitenothisp black qwage condqwage employed med_rent pct_poor [pw=p_pumawt] 
gen one=1

xpose, varname   clear

rename _varname name
replace name = "Age at Filing" if name=="age"
replace name = "Female" if name=="female"
replace name = "White, Not-Hispanic" if name=="whitenothisp"
replace name = "Hispanic" if name=="hisp"
replace name = "Black" if name=="black"
replace name = "Earnings" if name=="qwage" 
replace name = "Earnings, Conditional on Employed" if name=="condqwage" 
replace name = "Employed" if name=="employed" 
replace name = "Neighborhood Poverty Rate" if name=="pct_poor" 
replace name = "Neighborhood Median Rent" if name=="med_rent" 

tempfile means
save `means'
restore

collapse (sd) age female whitenothisp hisp black qwage condqwage employed med_rent pct_poor  
gen one=1

xpose, varname   clear

rename v1 sd
rename _varname name
replace name = "Age at Filing" if name=="age"
replace name = "Female" if name=="female"
replace name = "White, Not-Hispanic" if name=="whitenothisp"
replace name = "Hispanic" if name=="hisp"
replace name = "Black" if name=="black"
replace name = "Earnings" if name=="qwage" 
replace name = "Earnings, Conditional on Employed" if name=="condqwage" 
replace name = "Employed" if name=="employed" 
replace name = "Neighborhood Poverty Rate" if name=="pct_poor" 
replace name = "Neighborhood Median Rent" if name=="med_rent" 

merge 1:1 name using `means'
drop _merge

gen N=$bigN

gen evicted="ACS Sample"
rename v1 mean

gen location="`p'"

tempfile acs_`p'
save `acs_`p''

}

use `acs_nyc', clear
append using `acs_cc'
drop if name=="one"
order name mean sd N
save "${Inputs}/ACS_sumstats.dta", replace
